package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleMapper;
import com.itheima.pojo.Admin;
import com.itheima.pojo.Role;
import com.itheima.pojo.SysLog;
import com.itheima.service.RoleService;
import com.itheima.utils.*;
import com.itheima.vo.RoleVO;
import com.itheima.vo.SysLogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
//压制代码下面出现的灰色波浪线
@SuppressWarnings("Duplicates")
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private IdWorker idWorker;


    /*
    * 新增角色信息
    * */
    @Override
    public String addrole(RoleVO roleVO) {
        Role role = new Role();
        role.setRoleName(roleVO.getRoleName());//获取角色名
        List<Role> list = roleMapper.select(role);
        //判断角色是否存在
        if (list.size()>0){
            return ResultUtils.result("error", MessageConstant.ROLE_ADD_FAIL);
        }
        role.setUid(String.valueOf(idWorker.nextId()));
        role.setRoleName(roleVO.getRoleName());
        role.setCategoryMenuUids(roleVO.getCategoryMenuUids());
        role.setCreateTime(DateUtils.getNowTime());
        role.setUpdateTime(DateUtils.getNowTime());
        role.setStatus(1);
        role.setSummary(roleVO.getSummary());
        roleMapper.insert(role);
        return ResultUtils.result("success",MessageConstant.ROLE_ADD_SUCCESS);
    }

    /*
    * 删除角色信息
    * */
    @Override
    public void delete(RoleVO roleVO) {
        Role role = roleMapper.selectByPrimaryKey(roleVO);
        role.setStatus(0);//设置状态为0代表删除
        roleMapper.updateByPrimaryKeySelective(role);
    }

    /*
    * 更新角色信息
    * */
    @Override
    public Result edit(Role role) {
        Role roleOne = roleMapper.selectByPrimaryKey(role.getUid());
        if (roleOne==null){
            return new Result("error",MessageConstant.ROLE_ADD_FAIL);
        }
        role.setUpdateTime(DateUtils.getNowTime());
        roleMapper.updateByPrimaryKeySelective(role);
        return new Result("success",MessageConstant.ROLE_EDIT_SUCCESS);

    }

    /*
    * 获取角色信息列表
    * */

    @Override
    public Page<Role> findList(RoleVO roleVO) {
        if (roleVO!=null){
            //设置默认当前页
            if (roleVO.getCurrentPage() == null || "".equals(roleVO.getCurrentPage())) {
                roleVO.setCurrentPage(1);
            }
            //设置默认每页条数
            if (roleVO.getPageSize() == null || "".equals(roleVO.getPageSize())) {
                roleVO.setPageSize(10);
            }
        }else {
            roleVO = new RoleVO();
            roleVO.setCurrentPage(1);
            roleVO.setPageSize(10);
        }
        Integer currentPage = roleVO.getCurrentPage();
        Integer pageSize = roleVO.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        Example example = createExample(roleVO);
        return (Page<Role>) roleMapper.selectByExample(example);
    }
    @Override
    public int add(Role role) {
        Role role1 = new Role();
        role1.setRoleName(role.getRoleName());
        Role role2 = roleMapper.selectOne(role1);
        if (role2 !=null){
            return 0;
        }

        role.setCreateTime(DateUtils.getNowTime());
        role.setUpdateTime(DateUtils.getNowTime());
        role.setUid(String.valueOf(idWorker.nextId()));
        role.setStatus(1);

        int i = roleMapper.insert(role);
        return i;
    }


    private Example createExample(RoleVO roleVO){
        Example example = new Example(Role.class);
        Example.Criteria criteria = example.createCriteria();

        if (roleVO != null) {

            //角色介绍
            if (roleVO.getSummary() != null && "".equals(roleVO.getSummary())) {

                criteria.andLike("summary", "%" + roleVO.getSummary() + "%");
            }

            //角色名
            if (roleVO.getRoleName() != null && "".equals(roleVO.getRoleName())) {
                criteria.andLike("roleName", "%" + roleVO.getRoleName() + "%");
            }

        }
        return example;
    }
}
